package com.pks.datacenter.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.ohedu.base.model.UnitModel;
import org.ohedu.base.model.UserModel;
import org.ohedu.base.model.v2.AppModuleModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pks.datacenter.config.MyConfig;
import com.pks.util.UtilConvert;
import com.pks.web.dto.ServiceResult;
import com.thanone.base.common.model.DictionaryItemModel;
import com.thanone.base.common.model.IdentityGroupMemberModel;
import com.thanone.base.common.model.IdentityGroupModel;
import com.thanone.base.common.model.TeacherModel;

/**
 * 在正式环境中 请将 urlpath、api_account、api_password 提取到配置文件中进行配置 ，接口账号最好是一个应用一个帐号，云平台进行创建
 * 使用此工具类请导入 云平台的 model jar包 以及 fastJson (有提升转换速度) 工具包 
 * @author pks
 * @version 2018年1月25日
 */
public class YunApiUtil {
	
	private static Logger logger = LoggerFactory.getLogger(YunApiUtil.class);
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static YunApiToken yunApiToken;
	private static String urlpath = MyConfig.yun_api_url;
	private static String api_account = MyConfig.yun_api_account;
	
	private static String api_password = MyConfig.yun_api_password;
	public static Date afterTime;
	
	public static String getUrlpath() {
		return urlpath;
	}

	public void setUrlpath(String urlpath) {
		YunApiUtil.urlpath = urlpath;
	}

	public static String getApi_account() {
		return api_account;
	}

	public void setApi_account(String api_account) {
		YunApiUtil.api_account = api_account;
	}

	public static String getApi_password() {
		return api_password;
	}

	public void setApi_password(String api_password) {
		YunApiUtil.api_password = api_password;
	}
	
	/**
	 * 重新获取接口调用凭证
	 * 项目初始化请赋值 urlpath,api_account,api_password参数值
	 * 参数值解释：
	 * urlpath：接口地址（域名+端口） 例如：http://localhost:8080
	 * api_account：接口账号
	 * api_password：接口密码
	 * @return
	 * @author pks
	 * @date 2020年9月2日
	 */
	public static YunApiToken getApiTokenFromYun() {
		YunApiToken apiToken = new YunApiToken();
		String url = urlpath + "/httpapi/getToken.json" + "?account="+api_account+"&password="+api_password;
		String result = SUtilCommon.httpGetRequest(url);
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				String d = jsonObject.getString("d");
				apiToken = new Gson().fromJson(d, new TypeToken<YunApiToken>(){}.getType());
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败："+ result);
		}
		return apiToken;
	}
	
	/**
	 * 此方法因apitoken 信息保存在类中的对象，因此仅限单机部署环境使用，
	 * 如果使用了多台环境负载部署，请自行处理apitoken的共用
	 * @return
	 * @author pks
	 * @date 2020年9月2日
	 */
	public static YunApiToken getApiToken(){
		YunApiToken apiToken = YunApiUtil.yunApiToken;
		if(apiToken==null || apiToken.getEndtime()<new Date().getTime()){
			apiToken = getApiTokenFromYun();
			apiToken.setEndtime(System.currentTimeMillis()+apiToken.getEffective()-10*60*1000);
			YunApiUtil.yunApiToken = apiToken;
		}
		return apiToken;
	}
	
	/**
	 * 获取登录用户信息(全系统可以使用)
	 * @param userToken 回调地址携带的token信息
	 * @return
	 */
	public static UserModel findUserByUserToken(String apiToken,String userToken,Long moduleId){
		String url = urlpath + "/httpapi/findUserByUserToken.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(userToken)) params.put("token", userToken);
		if(moduleId!=null) params.put("moduleId", moduleId);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		System.err.println(result);
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				UserModel user = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<UserModel>(){}.getType());
				return user;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return null;
	}
	
	/**
	 * 获取登录用户信息(全系统可以使用)
	 * @param userId 用户id
	 * @return
	 */
	public static UserModel findUserById(String apiToken,Long id){
		
		String url = urlpath + "/httpapi/findUserById.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				UserModel user = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<UserModel>(){}.getType());
				return user;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return null;
	}
	
	
	/**
	 * 根据单位code 获取单位信息
	 * @param unitCode 单位编码
	 */
	public static UnitModel findUnitByCode(String apiToken,String unitCode){
		
		String url = urlpath + "/httpapi/findUnitByCode.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(unitCode))	params.put("unitCode", unitCode);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				UnitModel unit = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<UnitModel>(){}.getType());
				return unit;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return null;
	}
	

	/**
	 * 根据过滤条件分页查询用户列表 (包含单位信息)</br>
	 * order字段:
	 * <ul><li> 0: createDate desc</li>
	 * 			 <li>1: lastLoginDate desc</li>
	 * 			 <li>3: state asc</li>
	 * 			 <li>4: state desc</li>
	 * 			 <li>5: unitCode asc</li>
	 * 			 <li>6: role asc</li>
	 * 			 <li>7: sex asc</li></ul>
	 * @param search 查询关键字(查询字段由model界定)
	 * @param model  null表示search不起作用 0x1表示查找用户名 0x2表示查询真实名称   0x4表示拼音查找  0x8表示电话号码  0x10表示邮箱  0x20表示身份证 0x40表示qq
	 * @param unitCode 单位编号(由codeModel界定)
	 * @param codeModel null表示unitCode不起作用 ,0x1 表示 精确查询,  0x2表示 右模糊匹配，  0x3表示 左模糊匹配， 0x4 表示模糊匹配
	 * @param state 用户
	 * @param role 1表示教师  
	 * @param isDeleted 软删除标记
	 * @param offset 
	 * @param pageSize 每页查询数量
	 * @param order 排序
	 * @since 1.0
	 * @return
	 */
	public static List<UserModel> findUsersHaveUnitInfo(String apiToken,String search,Integer model,String unitCode,Integer codeModel,Integer state,Integer role,Boolean isDeleted,String userIdsJson,Boolean isIn,Integer offset,Integer pageSize,Integer order){
		
		String url = urlpath + "/httpapi/findUsersHaveUnitInfo.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(search)) params.put("search", search);
		if(model != null) params.put("model", model);
		if(StringUtils.isNotBlank(unitCode)) params.put("unitCode", unitCode);
		if(codeModel != null) params.put("codeModel", codeModel);
		if(state != null) params.put("state", state);
		if(role != null) params.put("role", role);
		if(isDeleted != null) params.put("isDeleted", isDeleted);
		if(StringUtils.isNotBlank(userIdsJson)) params.put("userIdsJson", userIdsJson);
		if(isIn != null) params.put("isIn", isIn);
		if(offset != null) params.put("offset", offset);
		if(pageSize != null) params.put("pageSize", pageSize);
		if(order != null) params.put("order", order);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<UserModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<UserModel>>(){}.getType());
				return list;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<UserModel>();
	}
	
	/**
	 * 根据过滤条件分页查询用户列表 (包含单位信息)</br>
	 * order字段:
	 * <ul><li> 0: createDate desc</li>
	 * 			 <li>1: lastLoginDate desc</li>
	 * 			 <li>3: state asc</li>
	 * 			 <li>4: state desc</li>
	 * 			 <li>5: unitCode asc</li>
	 * 			 <li>6: role asc</li>
	 * 			 <li>7: sex asc</li></ul>
	 * @param search 查询关键字(查询字段由model界定)
	 * @param model  null表示search不起作用 0x1表示查找用户名 0x2表示查询真实名称   0x4表示拼音查找  0x8表示电话号码  0x10表示邮箱  0x20表示身份证 0x40表示qq
	 * @param unitCode 单位编号(由codeModel界定)
	 * @param codeModel null表示unitCode不起作用 ,0x1 表示 精确查询,  0x2表示 右模糊匹配，  0x3表示 左模糊匹配， 0x4 表示模糊匹配
	 * @param state 用户
	 * @param role 1表示教师  
	 * @param isDeleted 软删除标记
	 * @param offset 
	 * @param pageSize 每页查询数量
	 * @param order 排序
	 * @since 1.0
	 * @return
	 */
	public static List<UserModel> findUsersNoUnit(String apiToken,String search,Integer model,String unitCode,Integer codeModel,Integer state,Integer role,Boolean isDeleted,String userIdsJson,Boolean isIn,Integer offset,Integer pageSize,Integer order){
		
		String url = urlpath + "/httpapi/findUsersNoUnit.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(search)) params.put("search", search);
		if(model != null) params.put("model", model);
		if(StringUtils.isNotBlank(unitCode)) params.put("unitCode", unitCode);
		if(codeModel != null) params.put("codeModel", codeModel);
		if(state != null) params.put("state", state);
		if(role != null) params.put("role", role);
		if(isDeleted != null) params.put("isDeleted", isDeleted);
		if(StringUtils.isNotBlank(userIdsJson))	params.put("userIdsJson", userIdsJson);
		if(isIn != null) params.put("isIn", isIn);
		if(offset != null) params.put("offset", offset);
		if(pageSize != null) params.put("pageSize", pageSize);
		if(order != null) params.put("order", order);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<UserModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<UserModel>>(){}.getType());
				return list;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<UserModel>();
	}
	
	/**
	 * 根据模块编号获取可以使用该模块的用户列表
	 * @param search 查询关键字(查询字段由model界定)
	 * @param searchModel  null表示search不起作用 0x1表示查找用户名 0x2表示查询真实名称   0x4表示拼音查找  0x8表示电话号码  0x10表示邮箱  0x20表示身份证 0x40表示qq 0x80表示单位名称
	 * @param 模块角色  1:管理员 , 99:普通用户
	 * @param moduleId
	 * @param isInThisModeul
	 * @param userIdList
	 * @param userIdIsIn
	 * @param offset
	 * @param pageSize
	 * @param order 排序 (预留字段,暂无实现)
	 * @return
	 */
	public static List<UserModel> findUsersByModuleId(String apiToken,String search,Integer model,Integer role,Long moduleId,Boolean isInThisModule,String userIdsJson,Boolean userIdIsIn,Integer offset,Integer pageSize,Integer order){
		String url = urlpath + "/httpapi/findUsersByModuleId.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(search))	params.put("search", search);
		if(model != null) params.put("model", model);
		if(role != null) params.put("role", role);
		if(moduleId != null) params.put("moduleId", moduleId);
		if(isInThisModule != null) params.put("isInThisModule", isInThisModule);
		if(StringUtils.isNotBlank(userIdsJson)) params.put("userIdsJson", userIdsJson);
		if(userIdIsIn != null) params.put("userIdIsIn", userIdIsIn);
		if(offset != null) params.put("offset", offset);
		if(pageSize != null) params.put("pageSize", pageSize);
		if(order != null) params.put("order", order);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<UserModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<UserModel>>(){}.getType());
				return list;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<UserModel>();
	}
	
	/**
	 * 根据过滤条件分页查询单位列表</br>
	 * order表示的值:
	 *   <ul>
	 *    <li>0:code desc</li>
	 *    <li>1:typeLevel1 asc</li>
	 *    <li>2:state asc</li>
	 *    <li>3:state desc</li>
	 *    <li>4:createDate desc</li>
	 *    <li>5:nameOfPingyin desc</li>
	 *    <li>6:typeLevel2 asc</li>
	 *    <li>7:typeLevel3 asc</li>
	 *   </ul>
	 * @param code  单位编号(匹配模式由codeModel来界定)
	 * @param codeModel 0x1表示 精确查询， 0x2 表示 左匹配模糊查询,  0x3 表示 右匹配模糊查询 ,0x4表示 模糊查询
	 * @param search  输入查询条件(通过model来界定查询字段)
	 * @param model  0x1 表示组织名称 ,0x2表示拼音,0x1|0x2表示所有
	 * @param parentId  上一级单位编号
	 * @param state  单位状态
	 * @param level2  组织二级类型
	 * @param level3 组织三级类型
	 * @param isDeleted  是否已经删除
	 * @param offset    offset
	 * @param pageSize  每页查询数
	 * @param order 排序  
	 * @param filterUnitIdList  需要过滤的列表,即单位编号列表
	 * @param isIn true 表示 in查询, false表示not in 查询
	 * @return
	 */
	public static List<UnitModel> findUnits(String apiToken,String code, Integer codeModel, String search, Integer model, Long parentId, Integer state, Integer level2, Integer level3, Boolean isDeleted,String filterUnitIdsJson,Boolean isIn,Integer offset, Integer pageSize, Integer order){
		String url = urlpath + "/httpapi/findUnits.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(code)) params.put("code", code);
		if(codeModel != null) params.put("codeModel", codeModel);
		if(StringUtils.isNotBlank(search)) params.put("search", search);
		if(model != null) params.put("model", model);
		if(parentId != null) params.put("parentId", parentId);
		if(state != null) params.put("state", state);
		if(level2 != null) params.put("level2", level2);
		if(level3 != null) params.put("level3", level3);
		if(isDeleted != null) params.put("isDeleted", isDeleted);
		if(StringUtils.isNotBlank(filterUnitIdsJson)) params.put("filterUnitIdsJson", filterUnitIdsJson);
		if(isIn != null) params.put("isIn", isIn);
		if(offset != null) params.put("offset", offset);
		if(pageSize != null) params.put("pageSize", pageSize);
		if(order != null) params.put("order", order);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<UnitModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<UnitModel>>(){}.getType());
				return list;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<UnitModel>();
	}
	
	/**
	 * @param moduleId 应用id
	 * @param userToken 
	 * @return
	 */
	public static Boolean checkModuleAdminByUserToken(String apiToken,Long moduleId,String userToken){
		Map<String, Object> params = new HashMap<String, Object>();
		if(moduleId != null) params.put("moduleId", moduleId);
		if(StringUtils.isNotBlank(userToken)) params.put("token", userToken);
		String url = urlpath + "/httpapi/isModuleAdmin.json?apiToken="+apiToken;
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				Boolean bool = jsonObject.getBoolean("d");
				return bool;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return null;
	}
	
	/**
	 * @param moduleId 应用id
	 * @param userId
	 * @return
	 */
	public static Boolean checkModuleAdminByUserId(String apiToken,Long moduleId,Long userId){
		Map<String, Object> params = new HashMap<String, Object>();
		if(moduleId != null) params.put("moduleId", moduleId);
		if(userId != null) params.put("userId", userId);
		String url = urlpath + "/httpapi/isModuleAdminByUserId.json?apiToken="+apiToken;
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				Boolean bool = jsonObject.getBoolean("d");
				return bool;
			}
		}else{
			logger.error("接口调用失败！");
		}
		return null;
	}
	
	/***
	 * 发送短信接口
	 * @param mobile 手机号
	 * @param content 短信内容
	 * @return
	 */
	public static ServiceResult sendMessage(String apiToken,String mobile,String content){
		String url = urlpath + "/httpapi/sendMessage.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("mobile", mobile);
		params.put("content", content);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			ServiceResult serviceResult = new Gson().fromJson(result, new TypeToken<ServiceResult>(){}.getType());
			if(!serviceResult.success()) {
				logger.error(serviceResult.getD().toString());
			}
			return serviceResult;
		}else{
			logger.error("接口调用失败！");
		}
		return ServiceResult.initError("发送失败！");
	}
	
	
	/**
	 * 上传云平台日志
	 * @param logLevel 日志级别 1、info  2、error 3、 debug 4、warn
	 * @param operater 操作人
	 * @param ip		操作人ip
	 * @param orgName	单位名称
	 * @param modId		应用id
	 * @param modName	应用名称
	 * @param infoModule	模块名称
	 * @param operationType	操作类型
	 * @param content	操作内容
	 * @return
	 */
	public static ServiceResult uploadLog(String apiToken,Integer logLevel,String operater,String ip,String orgName,Long modId,String modName,String infoModule,String operationType,String content){
		
		String url = urlpath + "/httpapi/uploadLog.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(logLevel != null) params.put("logLevel", logLevel);
		if(StringUtils.isNotBlank(operater)) params.put("operater", operater);
		if(StringUtils.isNotBlank(ip)) params.put("ip", ip);
		if(StringUtils.isNotBlank(orgName)) params.put("orgName", orgName);
		if(modId != null) params.put("modId", modId);
		if(StringUtils.isNotBlank(modName)) params.put("modName", modName);
		if(StringUtils.isNotBlank(infoModule)) params.put("infoModule", infoModule);
		if(StringUtils.isNotBlank(operater)) params.put("operationType", operationType);
		if(StringUtils.isNotBlank(content)) params.put("content", content);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			ServiceResult serviceResult = new Gson().fromJson(result, new TypeToken<ServiceResult>(){}.getType());
			return serviceResult;
		}else{
			logger.error("接口调用失败！");
		}
		return ServiceResult.initError("上传失败！");
	}
	
	/**
	 * 验证云平台用户帐号密码
	 * @param account 帐号 支持、昵称、手机号、身份证号
	 * @param password 密码
	 * @return
	 */
	public static ServiceResult checkAccount(String apiToken,String account,String password){
		String url = urlpath + "/httpapi/checkAccount.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(account)) params.put("account", account);
		if(StringUtils.isNotBlank(password)) params.put("password", password);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			ServiceResult serviceResult = new Gson().fromJson(result, new TypeToken<ServiceResult>(){}.getType());
			return serviceResult;
		}else{
			logger.error("接口调用失败！");
		}
		return ServiceResult.initError("验证失败！");
	}

	/**
	 * 根据组织id 获取单位信息
	 * @return
	 */
	public static UnitModel findUnitById (String apiToken,Long deptId) {
		String url = urlpath + "/httpapi/findUnitById.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", deptId);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				UnitModel unit = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<UnitModel>(){}.getType());
				return unit;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return null;
	}
	/***
	 * 根据字典key获取字典信息
	 * @param key
	 * @return
	 */
	public static List<DictionaryItemModel> findDicItemByKey (String apiToken,String key) {
		
		String url = urlpath + "/httpapi/findDicItemByKey.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(key))params.put("key", key);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<DictionaryItemModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<DictionaryItemModel>>(){}.getType());
				return list ;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<DictionaryItemModel>();
	}
	
	public static List<IdentityGroupMemberModel> findIdentityGroupMember (String apiToken,Long identityId,Long identityGroupId, Long userId) {
		String url = urlpath + "/httpapi/identityGroupMember.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(identityId!=null) params.put("identityId", identityId);
		if(identityGroupId!=null) params.put("identityGroupId", identityGroupId);
		if(userId!=null) params.put("userId", userId);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<IdentityGroupMemberModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<IdentityGroupMemberModel>>(){}.getType());
				return list ;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<IdentityGroupMemberModel>();
	}
	
	public static List<IdentityGroupModel> findIdentityGroupByIdentity (String apiToken,Long identityId){
		String url = urlpath + "/httpapi/findIdentityGroupByIdentity.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(identityId!=null) params.put("identityId", identityId);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<IdentityGroupModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<IdentityGroupModel>>(){}.getType());
				return list ;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<IdentityGroupModel>();
	}
	
	public static List<IdentityGroupMemberModel> findIdentityGroupMemberByUserIds(String apiToken,List<Long> userIds){
		String url = urlpath + "/httpapi/findIdentityGroupByIdentity.json?apiToken="+apiToken;
		Map<String, Object> params = new HashMap<String, Object>();
		if(userIds!=null) params.put("userIdsStr", UtilConvert.collection2String(userIds));
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<IdentityGroupMemberModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<IdentityGroupMemberModel>>(){}.getType());
				return list ;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<IdentityGroupMemberModel>();
	}
	
	public static List<UserModel> findModuleUsers(String apiToken,String search,Integer model,Integer role,
			Integer state,Date afterTime,Long moduleId,String userIdsJson,Boolean userIdIsIn,
			Integer offset,Integer pageSize,Integer order,Integer hasTeacherInfo){
		String url = urlpath + "/httpapi/findModuleUsers.json?apiToken=" + apiToken ;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("moduleId", moduleId);
		if(StringUtils.isNotBlank(search)) params.put("search", search);
		if(model != null) params.put("model", model);
		if(role != null) params.put("role", role);
		if(state != null) params.put("state", state);
		if(afterTime != null) params.put("afterTime", sdf.format(afterTime));
		if(StringUtils.isNotBlank(userIdsJson)) params.put("userIdsJson", userIdsJson);
		if(userIdIsIn != null) params.put("userIdIsIn", userIdIsIn);
		if(offset != null) params.put("offset", offset);
		if(pageSize != null) params.put("pageSize", pageSize);
		if(order != null) params.put("order", order);
		if(hasTeacherInfo != null) params.put("hasTeacherInfo", hasTeacherInfo);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<UserModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<UserModel>>(){}.getType());
				return list ;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<UserModel>();
	}
	
	public static TeacherModel findTeacherById(String apiToken,Long id){
		String url = urlpath + "/httpapi/findTeacherById.json?apiToken=" + apiToken ;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				TeacherModel user = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<TeacherModel>(){}.getType());
				return user;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return null;
	}
	
	public static List<TeacherModel> findTeachersOfUnit(String apiToken,Long orgId,Integer queryOrgMode,String userName,String realName,String mobilePhone,Integer order,Integer offset,Integer pageSize){
		String url = urlpath + "/httpapi/findTeachers.json?apiToken=" + apiToken ;
		Map<String,Object> params = new HashMap<String, Object>();
		if (orgId != null) params.put("orgId", orgId);
		if (queryOrgMode != null) params.put("queryOrgMode", queryOrgMode);
		if (StringUtils.isNotBlank(userName)) params.put("userName", userName);
		if (StringUtils.isNotBlank(realName)) params.put("realName", realName);
		if (StringUtils.isNotBlank(mobilePhone)) params.put("mobilePhone", mobilePhone);
		if (order != null) params.put("order", order);
		if (offset != null) params.put("offset", offset);
		if (pageSize != null) params.put("pageSize", pageSize);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<TeacherModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<TeacherModel>>(){}.getType());
				return list ;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<TeacherModel>();
	}

	/**
	 * 获取应用列表
	 * @param apiToken 接口token
	 * @param state 状态 1-上架 2-下架 3-维护 7-申请上架 8-开发 9-应用申请
	 * @return
	 */
	public static List<AppModuleModel> findAppModuleList(String apiToken, Integer state,Integer offset,Integer pagesize) {
		String url = urlpath + "/yunapi/appModuleList?apiToken=" + apiToken ;
		Map<String,Object> params = new HashMap<String, Object>();
		if (state != null) params.put("order", state);
		if (offset != null) params.put("offset", offset);
		if (pagesize != null) params.put("pagesize", pagesize);
		String result = SUtilCommon.httpPostRequest(url, SUtilCommon.paramstourl(params));
		if(StringUtils.isNotBlank(result)){
			JSONObject jsonObject = JSONObject.parseObject(result);
			if(1 == jsonObject.getIntValue("s")){
				List<AppModuleModel> list = new Gson().fromJson(jsonObject.getString("d"), new TypeToken<List<AppModuleModel>>(){}.getType());
				return list ;
			}else{
				logger.error("接口返回信息：" + result);
			}
		}else{
			logger.error("接口调用失败！");
		}
		return new ArrayList<AppModuleModel>();
	}
	
	
	
	
	
}

